package com.shoulder.boot.demo.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shoulder.boot.demo.dto.DemoAddDTO;
import com.shoulder.boot.demo.dto.DemoModDTO;
import com.shoulder.boot.demo.dto.DemoQueryDTO;
import com.shoulder.boot.demo.entity.Demo;
import com.shoulder.boot.demo.exception.DemoException;
import com.shoulder.boot.demo.mapper.Demo2Mapper;
import com.shoulder.boot.demo.mapper.DemoMapper;
import com.shoulder.boot.demo.service.DemoService;
import com.shoulder.boot.demo.utils.DemoUtils;
import com.shoulder.boot.demo.vo.DemoVo;
import com.shoulder.core.fetch.BaseIds;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * 样例信息 服务实现类
 *
 * @author 596767880@qq.com
 * @since 2024-06-04 14:36:24
 */
@Service
@Transactional
@AllArgsConstructor
public class DemoServiceImpl extends ServiceImpl<DemoMapper, Demo> implements DemoService {
    private final Demo2Mapper demo2Mapper;

    @Override
    public List<DemoVo> listQuery(DemoQueryDTO queryDTO) {
        List<Demo> list = this.baseMapper.selectList(DemoUtils.buildQueryWrapper(queryDTO));
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }
        return DemoUtils.buildUtils().toVos(list);
    }

    @Override
    public DemoVo look(String id) {
        Demo entity = this.baseMapper.selectById(id);
        if (ObjectUtils.isEmpty(entity)) {
            throw new DemoException("样例信息不存在");
        }
        return DemoUtils.buildUtils().toVo(entity);
    }

    @Override
    public List<DemoVo> looks(BaseIds<String> baseIds) {
        List<Demo> list = this.baseMapper.selectBatchIds(baseIds.getIds());
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }
        return DemoUtils.buildUtils().toVos(list);
    }

    @Override
    public void del(String id) {
        Demo entity = this.baseMapper.selectById(id);
        if (ObjectUtils.isEmpty(entity)) {
            throw new DemoException("样例信息不存在");
        }
        this.baseMapper.deleteById(id);
    }

    @Override
    public void batchDel(BaseIds<String> baseIds) {
        this.baseMapper.deleteBatchIds(baseIds.getIds());
    }


    @Override
    public Page<DemoVo> pageQuery(DemoQueryDTO queryDTO) {
        Page<Demo> selectPage = this.baseMapper.selectPage(new Page<>(queryDTO.getCurrent(), queryDTO.getSize())
                , DemoUtils.buildQueryWrapper(queryDTO));
        if (selectPage == null || CollectionUtils.isEmpty(selectPage.getRecords())) {
            return new Page<>();
        }
        return DemoUtils.buildUtils().toPage(selectPage);
    }

    @Override
    public void mod(DemoModDTO modDTO) {
        Demo info = Optional.ofNullable(this.baseMapper.selectById(modDTO.getId())).orElseThrow(() -> new DemoException("样例信息不存在"));
        BeanUtils.copyProperties(modDTO, info);
        this.baseMapper.updateById(info);
    }

    @Override
    public void add(DemoAddDTO addDTO) {
        Demo info = new Demo();
        BeanUtils.copyProperties(addDTO, info);
        this.baseMapper.insert(info);
    }

    @Override
    public List<DemoVo> demoTest() {
        return this.demo2Mapper.demoList();
    }
}